package per.hnvcam.searchext.services.impl;

import org.springframework.stereotype.Service;
import per.hnvcam.searchext.data.model.RuleData;
import per.hnvcam.searchext.services.ForecastService;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Vi Cam
 * Date: 08/01/2012
 * Time: 01:46
 * To change this template use File | Settings | File Templates.
 */
@Service("ForecastService")
public class ForecastServiceImpl implements ForecastService {

   @Override
   public long determineInitialRulesSetSize(int ruleLengthMinimum, int ruleLengthMaximum, List<RuleData> initialRuleSet) {
      long result = 0;
      for (RuleData baseRule : initialRuleSet) {
         for (int i = ruleLengthMinimum; i <= ruleLengthMaximum; i++) {
            result += getCombinationCount(baseRule.getConditions().size(), i);
         }
      }
      return result;
   }

   private long getCombinationCount(int size, int r) {
      long a = 1;
      long b = 1;
      int large = (size - r) > r ? size - r : r;
      int small = (size - r) > r ? r : size - r;
      for (int i = 1; i <= size; i++) {
         if (i <= small) {
            b *= i;
         }
         if (i > large) {
            a *= i;
         }
      }
      return a / b;
   }
}
